package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.RandomUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author ffk
 * @date 2024/5/25 16:34
 */
@Service
@Slf4j
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    DeviceDataMapper deviceDataMapper;


    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(),deviceDataPageReqDto.getPageSize());
        //前端传递的时间是一个long类型的毫秒值，作为条件查询数据库需要转换为日期类型
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();
        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null : LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null : LocalDateTimeUtil.of(endTime));

       List<DeviceDataVo>  pageList=deviceDataMapper.selectByPage(deviceDataPageReqDto);
       Page<DeviceDataVo> page= (Page<DeviceDataVo>) pageList;

        PageResponse<DeviceDataVo> of = PageResponse.of(page, DeviceDataVo.class);

        return of;
    }

    /**
     * 按日查询设备数据
     *
     * @param iotId      物联网设备id
     * @param functionId 功能id
     * @param startTime  开始时间  当天的00:00
     * @param endTime    结束时间
     * @return 设备数据
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {

        //按日聚合数据
        List<DeviceDataGraphVo> deviceDataGraphVoList = deviceDataMapper
                .queryDeviceDataListByDay(
                        iotId,
                        functionId,
                        LocalDateTimeUtil.of(startTime),
                        LocalDateTimeUtil.of(endTime));
        //构建一个24小时的集合
        List<DeviceDataGraphVo> list = DeviceDataGraphVo
                .dayInstance(LocalDateTimeUtil.of(startTime));
        //转换为map
        Map<String, Double> map = deviceDataGraphVoList
                .stream()
                .collect(
                        Collectors.toMap(DeviceDataGraphVo::getDateTime,
                                DeviceDataGraphVo::getDataValue));
        list.forEach(d -> {
            //从map获取值，如果为空，则给默认为0
            Double val = map.get(d.getDateTime()) == null ? RandomUtil.randomDouble(150,0, RoundingMode.HALF_UP) : map.get(d.getDateTime());


            d.setDataValue(val);
        });
        return list;
    }

    /**
     * 按周查询设备数据
     *
     * @param iotId      物联网设备id
     * @param functionId 功能id
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @return 设备数据
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        //按周聚合数据
        List<DeviceDataGraphVo> deviceDataGraphVoList = deviceDataMapper
                .queryDeviceDataListByWeek(iotId,
                        functionId,
                        LocalDateTimeUtil.of(startTime),
                        LocalDateTimeUtil.of(endTime));

        //构建一个7天的集合
        List<DeviceDataGraphVo> list = DeviceDataGraphVo.weekInstance(
                LocalDateTimeUtil.of(startTime));
        //转换为map
        Map<String, Double> map = deviceDataGraphVoList
                .stream()
                .collect(
                        Collectors.toMap(DeviceDataGraphVo::getDateTime,
                                DeviceDataGraphVo::getDataValue));

        list.forEach(d -> {
            //获取数据，如果为空，则补为0
            Double dataValue = Optional.ofNullable(map.get(d.getDateTime())).orElse(0.0);
            d.setDataValue(dataValue);
        });

        return list;
    }
}
